package com.bwie.service.impl;

import com.bwie.config.RedisConfig;
import com.bwie.exception.BusinessException;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.utils.SendMsgUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper ;

    @Autowired
    private HttpSession session ;

    //登录验证
    @Override
    public void doSmsLogin(User user) {
        String codeKey = "SMSCODE" + user.getPhone();//验证码key
        //开启jedis连接池
        JedisPool jedisPool = RedisConfig.redisConn();
        Jedis jedis = jedisPool.getResource();

        //手机号判空
        if(StringUtils.isEmpty(user.getPhone())
            || StringUtils.isEmpty(user.getSmsCode())){
            throw new BusinessException("手机号或验证码不能为空");
        }

        //手机号码校验
        if(!user.getPhone().matches("^1[3-9]\\d{9}$")){
            throw new BusinessException("手机号格式不正确");
        }

        //验证码格式正则校验
        if(!user.getSmsCode().matches("^[1-9]\\d{3}$")){
            throw new BusinessException("验证码格式不正确");
        }

        //判断手机号是否注册
        User userDao = userMapper.selectUserByPhone(user.getPhone());
        if(null == userDao){
            throw new BusinessException("手机号未注册,请先注册");
        }

        //比较(前端传的验证码=redis验证码)
        String redisCode = jedis.get(codeKey);
        if(StringUtils.isEmpty(redisCode)){
            throw new BusinessException("验证码已过期或未发送,请重新发送");
        }

        if(!redisCode.equals(user.getSmsCode())){
            throw new BusinessException("验证码有误,请重新输入");
        }

        //登录成功
        session.setAttribute("user",userDao);
        RedisConfig.closeJedisOrPool(jedis,jedisPool);
    }

    //发送验证码
    @Override
    public void sendSmsCode(String phone) {
        JedisPool jedisPool = RedisConfig.redisConn();
        Jedis jedis = jedisPool.getResource();
        String codeKey = "SMSCODE" + phone ;//验证码key
        String timeKey = "TIME" + phone ;//时间戳key

        /**
         * 非空校验
         */
        if(StringUtils.isEmpty(phone)){
            throw new BusinessException("手机号不能为空");
        }
        //手机号校验
        if(!phone.matches("^1[3-9]\\d{9}$")){
            throw new BusinessException("手机号格式不正确,请重新输入");
        }
        //生成验证码
        Random random = new Random();
        int i = random.nextInt(9000)+1000;  //1000-9000
        String smsCode = String.valueOf(i);
        //验证码必须是四位数字
        if(!smsCode.matches("^[1-9]\\d{3}$")){
            throw new BusinessException("验证码格式不正确");
        }

        //一分钟之内只能发送一次
        String timeStr = jedis.get(timeKey);
        if(StringUtils.isNotEmpty(timeStr)){
            Long redisTime = Long.valueOf(timeStr);
            long nowTime = new Date().getTime();
            long l = nowTime-redisTime ;//时间戳单位 毫秒
            if (l < 1 * 60 * 1000) {
                throw new BusinessException("1分钟内不能多次发送验证码");
            }
        }

        //5.发送
        String isOk = SendMsgUtil.sendSms(phone, smsCode);
//        if (!"OK".equals(isOk)) {
//            throw new BusinessException("发送失败");
//        }
        //6.验证码存入redis,设置1分钟过期

        jedis.setex(codeKey, 1 * 60, smsCode);//存验证码
        jedis.set(timeKey, String.valueOf(new Date().getTime()));//存时间戳
        RedisConfig.closeJedisOrPool(jedis, jedisPool);
    }
}
